<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <home></home>
  </div>
  <script src="./vue.js"></script>
  <script>
    // 在外部定义 第三方实例 bus
    const bus = new Vue();
    const Child1 = {
      template: `
        <div>
          <h3>这是子组件1</h3>  
          <button @click="emit">点击向我兄弟通信</button>
        </div>
      `,
      data(){
        return {
          msg: '这是子组件1中的数据'
        }
      },
      methods: {
        emit(){
          bus.$emit('piupiupiu', this.msg);
        }
      }
    }
    const Child2 = {
      template: `
        <div>
          <h3>这是子组件2</h3>  
          {{ msg }}
        </div>
      `,
      data(){
        return {
          msg: ''
        }
      },
      mounted(){
        /* bus.$on('piupiupiu', (msg) => {
          this.msg = msg;
        }) */
       /*  bus.$on('piupiupiu', function(msg){
          this.msg = msg;
        }.bind(this)) */
        const _this = this;
        bus.$on('piupiupiu', function(msg){
          _this.msg = msg;
        })
      }
    }
    Vue.component('Home', {
      template: `
        <div>
         <h2>父组件</h2>
          <hr/>
         <child1></child1>
         <hr/>
         <child2></child2>
        </div>
      `,
      components: {
        Child1,
        Child2
      }
    })
    const vm = new Vue({
      el: '#app'
    })
  </script>
</body>
</html>